//
//  ViewController.swift
//  函数式思想Battleship
//
//  Created by app on 2017/4/19.
//  Copyright © 2017年 app. All rights reserved.
//

import UIKit

struct Positon {
    var x: Double
    var y: Double
}
struct Ship {
    var postion:Positon
    var firingRange: Distace
    var unsafeRange: Distace
}
typealias Distace = Double
typealias Region = (Positon) -> Bool

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
  
}

extension Positon{
    func inRange(rang: Distace) -> Bool{
        return sqrt(x * x + y * y) <= rang
    }
    func minus(p: Positon) ->  Positon{
        return Positon(x: x - p.x, y: y - p.y)
    }
    var lehth: Double{
        return sqrt(x * x + y * y)
    }
}
extension Ship{
    
    func canEngageShip(target: Ship) -> Bool {
        let dx = target.postion.x - postion.x
        let dy = target.postion.y - postion.y
        let targetDistance = sqrt(dx * dx + dy * dy)
        return targetDistance <= firingRange && targetDistance > unsafeRange
    }
    
    func canSafelyEngageShip1(target: Ship, friendly: Ship) -> Bool {
        let dx = target.postion.x - postion.x
        let dy = target.postion.y - postion.y
        let targetDistance = sqrt(dx * dx + dy * dy)
        let friendlyDx = friendly.postion.x - target.postion.x
        let fridenlyDy = friendly.postion.y - target.postion.y
        let fridenlyDistance = sqrt(friendlyDx * friendlyDx + fridenlyDy * fridenlyDy)
        return targetDistance <= firingRange && targetDistance > unsafeRange && (fridenlyDistance > unsafeRange)
    }
    
    func canSafelyEngageShip2(target: Ship, friendly: Ship) -> Bool {
        
        let targetDistace = target.postion.minus(p: postion).lehth
        let friendltDistance = friendly.postion.minus(p: postion).lehth
        return targetDistace <= firingRange && targetDistace > unsafeRange && (friendltDistance > unsafeRange)
    }
}
